//
//  StoreViewController.m
//  cifNews_iphone
//
//  Created by  on 12-6-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "StoreViewController.h"
#import "CustomCell.h"
@interface StoreViewController ()

@end

@implementation StoreViewController

- (void)loadData
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    
    DataProvider *dp = [DataProvider sharedManager];
    DPData *dpData = [dp getBookList];
    
    if (dpData.isSuccess) {
        if ([[dpData.data objectForKey:@"json"] count]>0) {
            [bookList addObjectsFromArray:[dpData.data objectForKey:@"json"]];
            [self performSelectorOnMainThread:@selector(loadEnd) withObject:nil waitUntilDone:NO];
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle: nil 
                                                           message: @"无数据"
                                                          delegate: self 
                                                 cancelButtonTitle: @"确定" 
                                                 otherButtonTitles: nil];
            [alert show];
            [alert release];
        }
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle: nil 
                                                       message: dpData.data
                                                      delegate: self 
											 cancelButtonTitle: @"确定" 
                                             otherButtonTitles: nil];
		[alert show];
		[alert release];
    }
    [pool release];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ClearLoadingImage" object: nil];
}
-(void)loadEnd{
    
}
-(void)inittableView{
    UITableView*table=[[UITableView alloc] initWithFrame:CGRectMake(0, 55, 320, 460-45-55) style:UITableViewStylePlain];
    table.opaque=NO;
    table.backgroundColor=[UIColor clearColor];
    table.delegate=self;
    table.dataSource=self;
    table.allowsSelection=NO;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    [table release];
}
#pragma mark imagedelegate
- (void)imageDidFinishLoad:(UIImage *)image index:(NSInteger)index{
    
}

#pragma mark table
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString*identify=@"cell";
    CustomCell * cell=[tableView dequeueReusableCellWithIdentifier:identify];
    if (cell==nil) {
        cell=[[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify] autorelease];
    }
    int index=indexPath.row*2;
    if ((bookList.count%2)&&(indexPath.row==ceil(bookList.count/2.0))) {
        cell.colType=YES;
    }
    NSLog(@"%d",index);
    if (index<bookList.count) {
        NSDictionary* dic=[bookList objectAtIndex:index];
        NSDictionary* dic1=[bookList objectAtIndex:index+1];
        cell.bookview.titleLab.text=[dic valueForKey:@"mgTitle"];
        cell.bookview1.titleLab.text=[dic1 valueForKey:@"mgTitle"];
        cell.bookview.periodLab.text=[dic valueForKey:@"mgPeriod"];
        cell.bookview.periodLab.text=[dic valueForKey:@"mgPeriod"];
    }
    return  cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    double c=bookList.count/2.0;
    return ceilf(c);
}

#pragma mark -
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self inittableView];
    [self loadData];
	// Do any additional setup after loading the view.
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        bookList=[[NSMutableArray alloc] init];
    }
    return self;
}

- (void)dealloc{
    [bookList release];
    [super dealloc];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
